document.getElementById("clearBtn").addEventListener("click", async () => {
  // 获取当前激活的标签页
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  
  // 取出域名（含端口）
  const url = new URL(tab.url);
  const domain = url.hostname;

  // 拉取该域下所有 Cookie
  const cookies = await chrome.cookies.getAll({ domain });

  // 逐个删除
  const promises = cookies.map(c =>
    chrome.cookies.remove({
      url: (c.secure ? "https://" : "http://") + c.domain + c.path,
      name: c.name
    })
  );
  await Promise.all(promises);

  // 反馈
  document.getElementById("clearBtn").textContent = "已清除 ✓";
  setTimeout(() => window.close(), 800);
});